package com.data.util;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;

/**
 * @author helele
 * @date 2021/7/28
 */
@Component
public class InitDataBaseConnect {

//    private final String url = "jdbc:mysql://localhost:3306/sheet?useUnicode=true&characterEncoding=utf8?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true";

    private String ip = "localhost";
    private String port = "3306";
    private String database = "sheet";
    private String other = "useUnicode=true&characterEncoding=utf8?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true";

    private String url = "jdbc:mysql://" + ip + ":" + port + "/" + database + (StringUtils.hasText(other) ? "?" + other : "");


    private final String username = "easySheet";

    private final String password = "123654";

    private final String driverClassName = "com.mysql.cj.jdbc.Driver";

    private final Integer initialSize = 2;

    private final Integer minIdle = 1;

    private final Integer maxActive = 20;

    //最长等待时间
    private final Integer maxWait = 60000;

    private final Integer timeBetweenEvictionRunsMillis = 60000;

    private final Integer minEvictableIdleTimeMillis = 300000;

    private final Boolean testWhileIdle = true;

    private final Boolean testOnBorrow = false;

    private final Boolean testOnReturn = false;

    private final Boolean poolPreparedStatements = true;

    private final Integer maxPoolPreparedStatementPerConnectionSize = 20;


    @Bean
    public DataSource init() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        dataSource.setDriverClassName(driverClassName);
        //定义初始连接数
        dataSource.setInitialSize(initialSize);
        //最小空闲
        dataSource.setMinIdle(minIdle);
        //定义最大连接数
        dataSource.setMaxActive(maxActive);
        //最长等待时间
        dataSource.setMaxWait(maxWait);

        // 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);

        // 配置一个连接在池中最小生存的时间，单位是毫秒
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);

        // 打开PSCache，并且指定每个连接上PSCache的大小
        dataSource.setPoolPreparedStatements(poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        return dataSource;
    }

}
